package com.tang.transmission.controller;


import com.github.pagehelper.PageInfo;
import com.tang.common.entity.liveVideo.LiveInfo;
import com.tang.common.entity.liveVideo.VideoInfo;
import com.tang.common.exception.RecruitExceptionEnum;
import com.tang.common.uniform.Result;
import com.tang.transmission.service.LiveInfoService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 唐朝阳
 * @since 2022-05-17
 */
@RestController
@RequestMapping("/live")
public class LiveInfoController {
    @Autowired
    private LiveInfoService liveInfoService;

    @RequestMapping("/liveList")
    public Result getLiveList(String keywords, Integer currentPage, Integer pageSize) {
        PageInfo<LiveInfo> list = liveInfoService.listLiveInfo(keywords,currentPage,pageSize);
        if(list!=null){
            return Result.success(list);
        }else{
            return Result.error(RecruitExceptionEnum.NO_DATA);
        }
    }

    @RequestMapping("/getLive")
    public Result getVideo(Integer liveId) {
        LiveInfo live = liveInfoService.getById(liveId);
        if(live!=null){
            return Result.success(live);
        }else{
            return Result.error(RecruitExceptionEnum.NO_DATA);
        }
    }
    @RequestMapping("/companyLive")
    public Result companyLive(String keywords, Integer currentPage, Integer pageSize,String companyName) {
        PageInfo<LiveInfo> list = liveInfoService.listCompanyLive(keywords,currentPage,pageSize,companyName);
        if(list!=null){
            return Result.success(list);
        }else{
            return Result.error(RecruitExceptionEnum.NO_DATA);
        }
    }
    @SneakyThrows
    @RequestMapping("/appointLive")
    public Result appointLive(
            String userName,
            String companyName,
            String title,
           String content,
           String coverUrl,
           String beginTime,
            String speaker
    ) {
       if(userName!=null&&companyName!=null&&title!=null && beginTime!=null){
           LiveInfo liveInfo = new LiveInfo();
                   liveInfo.setUserName(userName);
           liveInfo.setInstitutions(companyName);
           liveInfo.setLiveTitle(title);
           liveInfo.setLiveIntroduce(content);
           liveInfo.setCoverUrl(coverUrl);
           SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           Date parse = simpleDateFormat.parse(beginTime);
           liveInfo.setBeginTime(parse);
           liveInfo.setSpeaker(speaker);
           boolean save = liveInfoService.save(liveInfo);
           if(save){
               return Result.success("预约成功");
           }
       }
       return Result.error(RecruitExceptionEnum.APPOINT_FAILED);
    }

    @SneakyThrows
    @RequestMapping("/updateLive")
    public Result updateLive(
            Integer liveId,
            String title,
            String content,
            String coverUrl,
            String beginTime,
            String speaker
    ) {
        LiveInfo liveInfo = liveInfoService.getById(liveId);
        if(liveInfo!=null){
            liveInfo.setLiveTitle(title);
            liveInfo.setLiveIntroduce(content);
            liveInfo.setCoverUrl(coverUrl);
            if(!beginTime.isEmpty()){
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date parse = simpleDateFormat.parse(beginTime);
                liveInfo.setBeginTime(parse);
            }
            liveInfo.setSpeaker(speaker);
            boolean save = liveInfoService.updateById(liveInfo);
            if(save){
                return Result.success("修改成功");
            }
        }
        return Result.error(RecruitExceptionEnum.UPDATE_FAILED);
    }
    @RequestMapping("/delLive")
    public Result delLiveById(String liveId) {

        LiveInfo liveInfo = liveInfoService.getById(liveId);
        liveInfo.setDeleteFlag(0);
        boolean result = liveInfoService.updateById(liveInfo);
        if(result){
            return Result.success("删除成功");
        }else{
            return Result.error(RecruitExceptionEnum.DELETE_FAILED);
        }
    }

    @RequestMapping("/liveCount")
    public Integer getLiveCount(){
        return liveInfoService.count(null);
    }


}
